package cn.stylefeng.guns.modular.config.controller;

import cn.stylefeng.guns.modular.config.entity.Config;
import cn.stylefeng.guns.modular.config.pojo.request.ConfigRequest;
import cn.stylefeng.guns.modular.config.service.ConfigService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@ApiResource(name = "官网配置")
public class ConfigController {
    @Resource
    ConfigService configService;

    @PostResource(name = "编辑", path = "/gwConfig/saveContactUs")
    public ResponseData edit(@RequestBody @Validated({ConfigRequest.edit.class}) ConfigRequest ConfigRequest) {
        Config email = new Config();
        email.setK("email");
        email.setV(ConfigRequest.getEmail());
        this.configService.edit(email);
        Config address = new Config();
        address.setK("address");
        address.setV(ConfigRequest.getAddress());
        this.configService.edit(address);
        Config phone = new Config();
        phone.setK("phone");
        phone.setV(ConfigRequest.getPhone());
        this.configService.edit(phone);
        Config addressMap = new Config();
        addressMap.setK("addressMap");
        addressMap.setV(ConfigRequest.getAddressMap());
        this.configService.edit(addressMap);
        Config wx1 = new Config();
        wx1.setK("wx1");
        wx1.setV(ConfigRequest.getWx1());
        this.configService.edit(wx1);
        Config wx2 = new Config();
        wx2.setK("wx2");
        wx2.setV(ConfigRequest.getWx2());
        this.configService.edit(wx2);

        return new SuccessResponseData();
    }

    @GetResource(name = "获取联系我们", path = "/gwConfig/getContactUs")
    public ResponseData getContactUs() {
        List<Config> configs = this.configService.list();
        Map<String, String> data = new HashMap<>();
        for (Config config : configs) {
            data.put(config.getK(), config.getV());
        }
        return new SuccessResponseData(data);
    }
}
